/*
 * lib2Desa - Another 2D game library in C++
 * Copyright (C) 2009 Erivaldo Xavier de Lima Filho
 *
 * This program is free software and is distributed under GNU General Public License version 3. You can modify and/or redistribute
 * it under the terms of GPL version 3 or posterior version. This program comes with NO WARRANTY, for more details, see LICENSE,
 * or <http://www.gnu.org/licenses>.
 *
 * Contact info:
 *
 *	Erivaldo Xavier de Lima Filho, desadoc@gmail.com
 *
 */

#include "Core/Game.hpp"

#ifndef _NO_NAMESPACE
namespace lib2Desa {
#endif

Game::Game(const shared_ptr<IGraphicDevice>& gd)
: g_dev(gd), scene(null), closeRule(null), sync(null)
{

}

Game::~Game(void)
{
	notif.clear();
}

void Game::setCloseRule(const shared_ptr<IGameRule>& r)
{
	closeRule = r;
}

void Game::setScene(const shared_ptr<Scene>& sc)
{
	scene = sc;
}

void Game::addEventNotifier(const shared_ptr<IEventNotifier>& enf)
{
	notif.push_back(enf);
}

void Game::setSync(const shared_ptr<ASync>& s)
{
	sync = s;
}

void Game::run(void)
{
	while(((closeRule)?closeRule->eval():true))
	{
		std::list<shared_ptr<IEventNotifier> >::iterator it = notif.begin();

		for(;it != notif.end(); it++)
		{
			(*it)->processEvents();
			(*it)->notifyListeners();
		}

		if(g_dev)
		{
			g_dev->clearScreen();

			if(scene)
			{
				scene->run(sync->getFrameTime());
				scene->draw(g_dev);
			}

			g_dev->refreshScreen();
			if(sync) sync->wait();
		}
	}
}

#ifndef _NO_NAMESPACE
}
#endif
